package com.danskin.wechat.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import org.apache.commons.lang3.StringUtils;
import org.jeewx.api.wxstore.stock.model.StockInfo;
import org.springframework.beans.factory.annotation.Autowired;

import com.danskin.wechat.cache.JedisClient;
import com.danskin.wechat.dao.StoreInfoDao;
import com.danskin.wechat.dto.MDMStoreInfoDto;
import com.danskin.wechat.entity.CrmBrandInfo;
import com.danskin.wechat.entity.StoreInfo;
import com.danskin.wechat.entity.example.StoreInfoExample;
import com.danskin.wechat.entity.example.StoreInfoExample.Criteria;
import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.service.CrmBrandInfoService;
import com.danskin.wechat.service.MDMStoreInfoService;
import com.danskin.wechat.service.SysAreaService;
import com.danskin.wechat.util.JsonUtils;
@WebService(endpointInterface = "com.danskin.wechat.service.MDMStoreInfoService") 
public class MDMStoreInfoServiceImpl implements MDMStoreInfoService {
	
	@Autowired
	private StoreInfoDao storeInfoDao;
	
	@Autowired
	private CrmBrandInfoService crmBrandInfoService;
	
	@Autowired
	private JedisClient jedisClient;
	
	@Autowired
	private SysAreaService sysAreaService;
	


	@Override
	public AccessResponse saveMDMStoreInfo(List<MDMStoreInfoDto> list) {
		AccessResponse<Object> access=new AccessResponse<Object>();
		List<StoreInfo> updateList=new ArrayList<StoreInfo>();
		List<StoreInfo> saveList=new ArrayList<StoreInfo>();
		int i=0;
		if(list.size()==0){
			access.setErrorCode("1");
			access.setErrorMsg("数据为空");
		}else{
			for(int k=0;k<list.size();k++){
				MDMStoreInfoDto storeInfoDto=list.get(k);
				StoreInfo storeInfo=new StoreInfo();
				//根据品牌ID获取mpId
				if(StringUtils.isNotBlank(storeInfoDto.getBUSINESSUNIT_CODE())){
					Map<String,Object> param=new HashMap<String, Object>();
					param.put("brandCode", storeInfoDto.getBUSINESSUNIT_CODE());
					CrmBrandInfo brandInfo = crmBrandInfoService.getCrmBrandInfoByCondition(param);
					if(null!=brandInfo){
						storeInfo.setMpId(brandInfo.getMpId());
					}else{
						storeInfo.setMpId(0);
					}
				}
				//开始组装数据
				storeInfo.setName(storeInfoDto.getSHOPNAME());
			    storeInfo.setStoreCode(storeInfoDto.getSHOPID());
			    if(null!=storeInfoDto.getPROVINCE_CODE()){
			    	String provice = sysAreaService.getStandarCodeByMDMCode(storeInfoDto.getPROVINCE_CODE());
			    	storeInfo.setProvince(provice);
			    }
			    if(null!=storeInfoDto.getCITY_CODE()){
			    	String city = sysAreaService.getStandarCodeByMDMCode(storeInfoDto.getCITY_CODE());
			    	storeInfo.setCity(city);
			    }
			    if(null!=storeInfoDto.getCOUNTY_CODE()){
			    	String district = sysAreaService.getStandarCodeByMDMCode(storeInfoDto.getCOUNTY_CODE());
			    	storeInfo.setDistrict(district);
			    }
			    storeInfo.setTelephone(storeInfoDto.getMOBILE());
			    storeInfo.setOpenTime(storeInfoDto.getOPENDATE());
			    storeInfo.setCloseTime(storeInfoDto.getCLOSEDATE());
			    storeInfo.setAddress(storeInfoDto.getSHOPADDRESS());
			    storeInfo.setType(Byte.valueOf("2"));
			    if(StringUtils.isNotBlank(storeInfoDto.getSHOPID())){
			    	StoreInfoExample example=new StoreInfoExample();
				    Criteria criteria = example.createCriteria();
			    	criteria.andStoreCodeEqualTo(storeInfoDto.getSHOPID());
			    	List<StoreInfo> list2 = storeInfoDao.selectByExample(example);//判断数据库中是否有该数据
			    	if(list2.size()>0){//数据库存在此数据 跟新
			    		updateList.add(storeInfo);//放到缓存的List中
			    	}else{
			    		saveList.add(storeInfo);
			    	}
			    }
			}
			jedisClient.set("MDMReceiveDate"+":"+"Original", JsonUtils.objectToJson(list));
			jedisClient.set("MDMReceiveDate"+":"+"UpdateList", JsonUtils.objectToJson(updateList));
			jedisClient.set("MDMReceiveDate"+":"+"SaveList", JsonUtils.objectToJson(saveList));
			jedisClient.expire("MDMReceiveDate"+":"+"Original", 86400);
			jedisClient.expire("MDMReceiveDate"+":"+"UpdateList", 86400);
			jedisClient.expire("MDMReceiveDate"+":"+"SaveList", 86400);
			if(updateList.size()>0){
			for (StoreInfo storeInfo : updateList) {
				StoreInfoExample example=new StoreInfoExample();
			    Criteria criteria1 = example.createCriteria();
				 criteria1.andStoreCodeEqualTo(storeInfo.getStoreCode());
				 i = storeInfoDao.updateByExampleSelective(storeInfo, example);
			}}
			
			if(saveList.size()>0){
				i = storeInfoDao.insertBatchSelective(saveList);
			}
			     
			if(i>0){
				access.setErrorCode("0");
				access.setErrorMsg("接收数据成功");
			}
		}
		
		return access;
	}


	


}
